import CommonTable from "@/pages/OrderModule/common/components/CommonTable/components/CommonTable";
import CommonText from "@/pages/OrderModule/common/components/CommonText/CommonText";
import SubTitle from "@/pages/OrderModule/common/components/SubTitle";
import { Button, Space, Switch, message, Popconfirm } from "antd";
import { memo, useState, useEffect } from "react";
import CommonLoading from "@/components/CommonLoading";
import CommonTag from "@/components/CommonTag";
import AddNotAutoReviewRule from "./AddNotAutoReviewRule";
import {
	getOrderRulesOp,
	updateOrderRulesOp,
	notAutoReviewStrategyList,
	deleteNotAutoReviewStrategy,
	updateStatusNotAutoReviewStrategy
} from "../../service";

const TagAutomateReview = ({ type }) => {
	const [baseConfigData, setBaseConfigData] = useState({}); //订单基础配置数据
	const [tableData, setTableData] = useState([]); //表格数据
	const [visible, setVisible] = useState(false); //新增/编辑弹窗
	const [visibleType, setVisibleType] = useState("add"); //弹窗的状态
	const [recordData, setRecordData] = useState({}); //单行的数据
	const [loading, setLoading] = useState(false);

	const { autoReview } = baseConfigData; //是否开启自动审核

	useEffect(() => {
		if (type == 4) {
			getBaseConfigData();
			getNotAutoReviewStrategyList();
		}
	}, [type]);

	//获取订单基础配置数据
	const getBaseConfigData = async () => {
		setLoading(true);
		const { success, data = {} } = await getOrderRulesOp();
		if (success) {
			setBaseConfigData(data);
		}
		setLoading(false);
	};

	//查询表格数据
	const getNotAutoReviewStrategyList = async () => {
		setLoading(true);
		setTableData([]);
		const { success, data = [] } = await notAutoReviewStrategyList({
			current: 1,
			size: 500
		});
		if (success) {
			setTableData(data);
		}
		setLoading(false);
	};

	//开关切换
	const handleSwitch = e => {
		upDateBaseConfigData({
			autoReview: e ? 1 : 0
		});
	};

	//修改订单基础配置数据
	const upDateBaseConfigData = async val => {
		setLoading(true);
		const { success, msg = "" } = await updateOrderRulesOp({
			id: baseConfigData.id,
			...val
		});
		if (success) {
			message.success(msg);
			getBaseConfigData();
		}
		setLoading(false);
	};

	//删除当前数据
	const handleDeleteItems = async record => {
		setLoading(true);
		const { msg = "", success } = await deleteNotAutoReviewStrategy({ id: record.id });
		if (success) {
			message.success(msg);
			getNotAutoReviewStrategyList();
		}
		setLoading(false);
	};

	//修改状态
	const handleUpdateStatus = async record => {
		setLoading(true);
		const { msg = "", success } = await updateStatusNotAutoReviewStrategy({ id: record.id, status: record.status == 1 ? 0 : 1 });
		if (success) {
			message.success(msg);
			getNotAutoReviewStrategyList();
		}
		setLoading(false);
	};

	//单行点击编辑
	const handleEditItem = (record, type) => {
		setVisible(true);
		setRecordData(record);
		setVisibleType([type]);
	};

	const tableColumns = [
		{
			title: "序号",
			dataIndex: "index",
			key: "index",
			width: 60,
			render: (t, r, i) => i + 1
		},
		{
			title: "不自动审核规则名称",
			dataIndex: "name",
			key: "name",
			width: 120
		},
		{
			title: "指定店铺",
			dataIndex: "ecStoreNames",
			key: "ecStoreNames",
			width: 120,
			render: (text, r, i) => <span>{text.length ? text.join(",") : "/"}</span>
		},
		{
			title: "优先级",
			dataIndex: "level",
			key: "level",
			width: 120
		},
		{
			title: "状态",
			dataIndex: "status",
			key: "status",
			width: 120,
			render(value) {
				return <CommonText text={value ? "已开启" : "未开启"} type={value ? "success" : "error"} />;
			}
		},
		{
			title: "查看规则",
			dataIndex: "level",
			key: "level",
			width: 120,
			render: (t, record, i) => {
				return (
					<>
						<Button onClick={() => handleEditItem(record, "view")} type="link" size="small">
							查看规则
						</Button>
					</>
				);
			}
		},
		{
			title: "操作",
			width: 120,
			render: (t, record, i) => {
				return (
					<>
						<Button onClick={() => handleEditItem(record, "edit")} type="link" size="small">
							编辑
						</Button>
						<Popconfirm
							title={`确定${record.status == 1 ? "关闭" : "开启"}?`}
							onConfirm={() => handleUpdateStatus(record)}
							okText="确定"
							cancelText="取消"
						>
							<Button type="link" size="small">
								{record.status == 1 ? "关闭" : "开启"}
							</Button>
						</Popconfirm>
						<Popconfirm title="确定删除?" onConfirm={() => handleDeleteItems(record)} okText="确定" cancelText="取消">
							<Button type="link" size="small">
								删除
							</Button>
						</Popconfirm>
					</>
				);
			}
		}
	];

	//新增编辑弹窗
	const AddNotAutoReviewRuleProps = {
		visible,
		type: visibleType,
		recordData,
		handleCancel() {
			setVisible(false);
			setVisibleType("add");
			setRecordData({});
		},
		handleOk() {
			getNotAutoReviewStrategyList();
			setVisible(false);
			setVisibleType("add");
			setRecordData({});
		}
	};

	return (
		<Space direction="vertical" size={20}>
			{/* <SubTitle
				title={
					<>
						是否开启自动审核：
						<Switch
							checkedChildren="已开启"
							unCheckedChildren="已关闭"
							checked={autoReview}
							onClick={e => handleSwitch(e)}
							size="default"
						/>
					</>
				}
				symbol
				as="s2"
			/> */}

			<div>
				对不会自动审核的订单或者审核失败的订单显示标签： <CommonTag type="notAutoCheck" />
			</div>
			<Button
				onClick={() => {
					setVisible(true);
				}}
			>
				新增不自动审核规则
			</Button>
			<SubTitle title="满足以下条件的不会自动审核" symbol as="s2" />
			<CommonTable columns={tableColumns} dataSource={tableData} pagination={false} scroll={{ y: 500 }} rowKey={"id"} />
			{visible && <AddNotAutoReviewRule {...AddNotAutoReviewRuleProps} />}
			<CommonLoading loading={loading} />
		</Space>
	);
};

export default memo(TagAutomateReview);
